# -*- coding: UTF-8 -*-

import gconf

class ImageshackGconfClient(object):
    applet_dir = "/apps/imageshack.us-applet"
    display_key = "/apps/imageshack.us-applet/display"
    service_key = "/apps/imageshack.us-applet/service"
    codes_dir = "/apps/imageshack.us-applet/codes"

    def __init__(self, callback=None, callback2=None):
        """
        GConf client for getting/setting

        @param callback: callback function. Executing
            when proxy state changed. It calls with params:
              * client - GConf client
              * cnxn_id - connection ID
              * entry - changed entry
              * params - additional params
        @type callback: callable
        """
        if callback is None:
            callback = lambda client, cnxn_id, entry, params: None
        if callback2 is None:
            callback2 = lambda client, cnxn_id, entry, params: None
        # make connection to GConfD
        self.client = gconf.client_get_default()
        # add display_dir for inspection, without preload
        self.client.add_dir(self.applet_dir,
                            gconf.CLIENT_PRELOAD_NONE)
        # add callback for notifying about changes
        self.client.notify_add(self.display_key,
                               callback)
        self.client.notify_add(self.service_key,
                               callback2)

    def get_display(self):
        return self.client.get_int(self.display_key)

    def set_display(self, value):
        if not self.client.set_int(self.display_key,
                                      value):
            raise RuntimeError("Unable to change key %d" % \
                               self.display_key)
                               
    def get_service(self):
        service = self.client.get_string(self.service_key)
        if service is None:
            service = "Imageshack.us"
            self.set_service("Imageshack.us")
        return service

    def set_service(self, value):
        if not self.client.set_string(self.service_key,
                                      value):
            raise RuntimeError("Unable to change key %s" % \
                               self.service_key)
